import sys, random, os, Numeric;

exp_dir = '/projectsloc/svilen/vldb2008_1/exp';
top_dir = exp_dir+'/top';
tree_dir = exp_dir+'/trees/full';

routedir_list = [];

top = {
	'100-1-sq': 100, '100-1-sq-broken-10-1': 100, '100-1-sq-broken-21-1': 100,
   	'100-n-1-1': 100, '100-n-1-2': 100, '100-n-1-3': 100, 
   	'100-n-2-1': 100, '100-n-2-2': 100, '100-n-2-3': 100,
   	'100-n-3-1': 100, '100-n-3-2': 100, '100-n-3-3': 100, 
   	'100-n-4-1': 100, '100-n-4-2': 100, '100-n-4-3': 100,
   	'250-n-1': 250, '200-n-1': 200, '50-n-1': 50, '55-intel': 55
      };

cf = {
	'node_topology':		top_dir+'/errors-100-n-4-1.txt',
	'dir_value': 			top_dir+'/values-100-n-4-1',
	'dir_tree': 			'trees',
	'dir_join': 			'joins',
	'dir_route': 			'route',
	'node_count': 			'100',
        'attr_desc':                    'id exp2 uniform exp1 exp2 exp2 small_grid large_grid coord z*',
        'summary_desc':                 'id(id,B,0) exp2(exp2,B,0) uniform(uniform,B,0) exp1(exp1,B,0) id_repair(id,B,1) exp2_hc(exp2,C,10) small_grid(small_grid,B,0) large_grid(large_grid,B,0) coord(coord,R,0)',
	'dir_sample': 			top_dir+'/samples',
	'load_trees': 			'0',
	'route_begin': 			'0',
	'route_method': 		'1',
	'route_treesenabled': 		'3',
	'route_disableshortcuts': 	'0',
	'route_disablepayload': 	'0',
        'query_type':                   '2',
	'query_args': 			'25 50',
        'query_dynamicattr':            'z',
	'query_invjoinsel': 		'10',
	'query_invsrcsel': 		'2 2',
	'query_samplingtime': 		'0 10000',
	'query_sendtoroot': 		'0',
	'query_prune': 			'0',
	'query_multicast': 		'1',
	'query_cachemulticast': 	'1',
	'query_samplecycles': 		'100',
	'pause': 			'0',
	'disable_msglog': 		'1',
	'query_populatewindow':         '0',
        'query_windowsize':             '5',
        'retry_count':                  '200',
        'merge_paths':                  '0',
	'group_opt':			'0',
        'config_enableght':             '0',
        'query_dimensions':             '60 60',
	'confirm_drop':                 '1',
	'disable_debugout':             '0',
	'repair_level':                 '1',
	'query_estsel':                 '0',
	'config_sigmod07':              '0'
     };


def execute_config(cf):
	global routedir_list;
	routedir_list[len(routedir_list):] = [cf['dir_route']];

        f = open('config.txt', 'w');
        for k, v in sorted(cf.iteritems()):
		try:
	                f.write(k+'='+v+'\n');
		except:
			print("Cannot write configuration!");
			print(k);
			print(v);
			sys.exit(1);

        f.close();

	cmd = 'python test_script.py';
	if (cf['disable_debugout'] == '0'):
		cmd = cmd + ' > debug_out.txt';
	else:
		cmd = cmd + ' > /dev/null';

	error_code = os.system(cmd);
	if (error_code == 0):
	        os.system('mv debug_out.txt msg_rcv.log cmd_rcv.log msg_sent.log cmd_sent.log cmd_sentconf.log cmdhi_sent.log cmdhi_rcv.log cmd_drop.log '+cf['dir_route']+'/');
	else:
		print('Error code '+str(error_code)+' returned by sim!');
		sys.exit(1);


def build_trees(test_topologies, tc_index):
	for top_name, n in top.iteritems():
		if top_name not in test_topologies:
			continue;

	        cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
        	cf['node_count'] = str(n);
	        cf['dir_value'] = top_dir+'/values-'+top_name;

		for tci in tc_index:
		        cf['dir_tree'] = 'trees-'+top_name+'/tc'+tci;
		        execute_config(cf);


def test_allpairroute(test_topologies, tci_index, test_treesenabled, test_shortcuts, test_ght, dim, batch_size, batch_start, batch_end):
	for top_name, n in top.iteritems():
        	if top_name not in test_topologies:
                	continue;

	        cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
        	cf['node_count'] = str(n);
        	cf['dir_value'] = top_dir+'/values-'+top_name;

                for tci in tci_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci;

                        cf['load_trees'] = '1';
                        cf['route_begin'] = '1';
                        cf['query_type'] = '0';
                        cf['route_method'] = '1';
                        cf['route_disablepayload'] = '1';

                        cf['config_enableght'] = test_ght;
                        cf['query_dimensions'] = dim;

                        for cf['route_treesenabled'] in test_treesenabled:
                                for cf['route_disableshortcuts'] in test_shortcuts:
                                        random.seed(0);
                                        pair_used = Numeric.zeros(n*n, 'i');
                                        ca = '';

                                        for i in range(0, n*n):
                                                k = random.randint(0, n*n-1);
                                                while pair_used[k] != 0:
                                                        k = random.randint(0, n*n-1);

                                                pair_used[k] = 1;
                                                ca = ca+' '+str(k % n)+' '+str(k / n);

                                                if i % batch_size == batch_size-1:
                                                        cf['query_args'] = ca;
                                                        cf['dir_route'] = 'allpair_route-'+top_name+'/tc'+tci+'/t'+cf['route_treesenabled']+'/s'+cf['route_disableshortcuts']+'/route'+str(i/batch_size);

                                                        if (batch_start < 0) | (batch_end < 0) | ((i/batch_size >= batch_start) & (i/batch_size <= batch_end)):
                                                                print(top_name+' Batch '+str(i/batch_size)+' ('+cf['route_treesenabled']+', '+cf['route_disableshortcuts']+'): '+ca);
                                                                execute_config(cf);

                                                        ca = '';


def test_spatialroute(test_topologies, tc_index, test_treesenabled, test_shortcuts):
        for top_name, n in top.iteritems():
		if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;

                for tci in tc_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci;

                        cf['load_trees'] = '1';
                        cf['route_begin'] = '1';
                        cf['route_method'] = '1';
                        cf['route_disablepayload'] = '1';

                        cf['query_type'] = '1';
                        for cf['route_treesenabled'] in test_treesenabled:
                                for cf['route_disableshortcuts'] in test_shortcuts:
                                        for test_index in (0, 1):
                                                cf['query_args'] = ('40 100', '0 12')[test_index];
                                                test_directory = 'spatial_route-'+top_name+'/tc'+tci+'/t'+cf['route_treesenabled']+'/s'+cf['route_disableshortcuts']+'/q'+cf['query_type']+'/fringe'+str(test_index);
                                                cf['dir_route'] = test_directory+'/route0';

                                                print('Executing: '+cf['dir_route']);
                                                execute_config(cf);
                                                os.system('~/analyze.exe '+test_directory);

                        cf['query_type'] = '2';
                        for cf['route_treesenabled'] in test_treesenabled:
                                for cf['route_disableshortcuts'] in test_shortcuts:
                                        cf['query_args'] = '25 50';
                                        test_directory = 'spatial_route-'+top_name+'/tc'+tci+'/t'+cf['route_treesenabled']+'/s'+cf['route_disableshortcuts']+'/q'+cf['query_type'];
                                        cf['dir_route'] = test_directory+'/route0';

                                        print('Executing: '+cf['dir_route']);
                                        execute_config(cf);
                                        os.system('~/analyze.exe '+test_directory);


def set_joinquery(query_type, window_size, sampling_time, sample_cycles):
		cf['query_type'] = query_type;

                if (query_type == '0'):
                        cf['query_args'] = '16 39 1 62 99 33 10 53 90 42 91 66 32 9 41 95 44 18 71 80';
		elif (query_type == '2'):
	                cf['query_args'] = '25 50';
#			cf['query_args'] = '25 90';
		elif (query_type == '5'):
	                cf['query_args'] = '';
		elif (query_type == '6'):
			cf['query_args'] = '50 50';
		else:
			cf['query_args'] = '';

                cf['query_populatewindow'] = '1';
                cf['query_windowsize'] = window_size;
		cf['query_samplingtime'] = '0 '+str(sampling_time);
		cf['query_samplecycles'] = str(sample_cycles);

                cf['route_method'] = '1';
                cf['route_disablepayload'] = '0';
                cf['route_treesenabled'] = '3';
                cf['route_disableshortcuts'] = '0';


def decode_method(method_index):
	join_config = (('1','0','0','0','0','0','0','0'), ('0','1','0','0','0','0','0','0'), ('0','0','0','0','0','0','0','0'), 
		('0','0','1','1','0','0','0','0'), ('0','0','1','1','2','0','0','0'), ('0','0','1','1','0','1','0','0'), 
		('0','0','1','1','2','1','0','0'), ('0','0','0','0','0','0','1','0'), ('1','0','0','0','0','0','0','1'));
	return join_config[method_index];


def test_joinmethods(test_topologies, tc_index, srcsel_config, join_sel, test_methods, query_type, window_size, sampling_time, sample_cycles):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;

                for tci in tc_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci;
                        cf['load_trees'] = '1';

                        set_joinquery(query_type, window_size, sampling_time, sample_cycles);

                        for real_sel in srcsel_config:
                                cf['query_invsrcsel'] = str(real_sel[0])+' '+str(real_sel[1]);

                                for jointest_index in test_methods:
					method_settings = decode_method(jointest_index);
                                        cf['query_sendtoroot'] = method_settings[0];
                                        cf['query_prune'] = method_settings[1];
                                        cf['query_multicast'] = method_settings[2];
                                        cf['query_cachemulticast'] = method_settings[3];
                                        cf['merge_paths'] = method_settings[4];
                                        cf['group_opt'] = method_settings[5];
                                	cf['config_enableght'] = method_settings[6];
                                	cf['config_sigmod07'] = method_settings[7];

					if (cf['config_sigmod07'] == '1'):
						cf['confirm_drop'] = '3';
					else:
						cf['confirm_drop'] = '1';
					

                                        for cf['query_invjoinsel'] in join_sel:
                                                common_dirname = top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/ws'+str(window_size)+'/method'+str(jointest_index);
                                                cf['dir_join'] = 'joinstate-'+common_dirname;
                                                print('Executing: '+cf['dir_join']);

                                                cf['route_begin'] = '1';
                                                cf['dir_route'] = 'joinroute_est-'+common_dirname;

                                                print('Establishing paths...');
                                                execute_config(cf);

                                                cf['route_begin'] = '0';
                                                cf['dir_route'] = 'joinroute_sampling-'+common_dirname;

                                                if ((cf['query_sendtoroot'] == '1') & ((real_sel[0] == '1') | (real_sel[1] == '1'))):
                                                        cf['query_samplecycles'] = str(2*sample_cycles);
                                                        cf['query_samplingtime'] = '0 '+str(2*sampling_time);
						else:
							cf['query_samplecycles'] = str(sample_cycles);
                                                        cf['query_samplingtime'] = '0 '+str(sampling_time);

                                                print('Sampling...');
                                                execute_config(cf);


def test_joinmethods_learn(test_topologies, tc_index, srcsel_config, join_sel, query_type, window_size, sampling_time, sample_cycles, groupsel, learn):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;

                for tci in tc_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci;
                        cf['load_trees'] = '1';

                        set_joinquery(query_type, window_size, sampling_time, sample_cycles);

                        cf['query_estsel'] = '0';
                        cf['config_groupsel'] = groupsel[0];

                        cf['query_sendtoroot'] = '0';
                        cf['query_prune'] = '0';
                        cf['query_multicast'] = '1';
                        cf['query_cachemulticast'] = '1';
                        cf['merge_paths'] = '2';
                        cf['group_opt'] = '1';
                        cf['config_enableght'] = '0';
                        cf['config_sigmod07'] = '0';

                        cf['query_invsrcsel'] = str(srcsel_config[0][0])+' '+str(srcsel_config[0][1]);
                        cf['query_invjoinsel'] = join_sel[0];
                        cf['query_invsrcsel_new'] = str(srcsel_config[2][0])+' '+str(srcsel_config[2][1]);
                        cf['query_invjoinsel_new'] = join_sel[2];

                        common_dirname = top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+join_sel[0]+"-"+join_sel[1]+"-"+join_sel[2]+'/ss'+srcsel_config[0][0]+"-"+srcsel_config[0][1]+"-"+srcsel_config[1][0]+"-"+srcsel_config[1][1]+"-"+srcsel_config[2][0]+"-"+srcsel_config[2][1]+'/ws'+str(window_size)+'/est'+learn+'/gs'+groupsel[0]+"-"+groupsel[1];
                        cf['dir_join'] = 'joinstate-'+common_dirname;
                        print('Executing: '+cf['dir_join']);

                        cf['route_begin'] = '1';
                        cf['dir_route'] = 'joinroute_est-'+common_dirname;

                        print('Establishing paths...');
                        execute_config(cf);

                        cf['query_invsrcsel'] = str(srcsel_config[1][0])+' '+str(srcsel_config[1][1]);
                        cf['query_invjoinsel'] = join_sel[1];

                        cf['route_begin'] = '0';
                        cf['dir_route'] = 'joinroute_sampling-'+common_dirname;

                        cf['query_samplecycles'] = str(sample_cycles);
                        cf['query_samplingtime'] = '0 '+str(sampling_time);

                        cf['query_estsel'] = learn;
                        cf['config_groupsel'] = groupsel[1];
                        if (groupsel[1] == '0'):
                        	cf['query_seltransition'] = str(sampling_time/2);
			else:
				cf['query_seltransition'] = '0';

                        print('Sampling...');
                        execute_config(cf);


def test_joinvar(test_topologies, tc_index, realsel_config, joinpointsel_config, join_sel, test_methods, query_type, window_size, sampling_time, sample_cycles, est_sel):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
		cf['query_estsel'] = est_sel;

                for tci in tc_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci;
                        cf['load_trees'] = '1';

                        set_joinquery(query_type, window_size, sampling_time, sample_cycles);

			for jointest_index in test_methods:
	                        method_settings = decode_method(jointest_index);
        	                cf['query_sendtoroot'] = method_settings[0];
                	        cf['query_prune'] = method_settings[1];
                        	cf['query_multicast'] = method_settings[2];
	                        cf['query_cachemulticast'] = method_settings[3];
        	                cf['merge_paths'] = method_settings[4];
                	        cf['group_opt'] = method_settings[5];
 				cf['config_enableght'] = method_settings[6];
				cf['config_sigmod07'] = method_settings[7];

                                if (cf['config_sigmod07'] == '1'):
                                	cf['confirm_drop'] = '0';
                                else:
                                	cf['confirm_drop'] = '1';


                        	for cf['query_invjoinsel'] in join_sel:
                                	for joinpoint_sel in joinpointsel_config:
                                        	cf['route_begin'] = '1';
	                                        cf['query_invsrcsel'] = str(joinpoint_sel[0])+' '+str(joinpoint_sel[1]);
        	                                common_dirname = top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(joinpoint_sel[0])+'-'+str(joinpoint_sel[1])+'/ws'+str(window_size)+'/method'+str(jointest_index);
	                                        cf['dir_join'] = 'joinstate-var-'+common_dirname;
        	                                cf['dir_route'] = 'joinroute_est-var-'+common_dirname;

                	                        print('Executing: '+cf['dir_route']);
                        	                print('Establishing paths...');
                                	        execute_config(cf);

                                        	for real_sel in realsel_config:
                                                	cf['route_begin'] = '0';
	                                                cf['query_invsrcsel'] = str(real_sel[0])+' '+str(real_sel[1]);
        	                                        cf['dir_route'] = 'joinroute_sampling-var-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+str(cf['query_invjoinsel'])+'/ss_real'+str(real_sel[0])+'-'+str(real_sel[1])+'/ss_used'+str(joinpoint_sel[0])+'-'+str(joinpoint_sel[1])+'/ws'+str(window_size)+'/method'+str(jointest_index);

	                                                print('Executing: '+cf['dir_route']);
        	                                        print('Sampling...');
                	                                execute_config(cf);


def route_intel(tc_index, test_methods, est_sel):
	for top_name, n in top.iteritems():
                if 'intel' not in top_name:
			continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
		cf['query_estsel'] = est_sel;

                for tci in tc_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci;
                        cf['dir_sample'] = top_dir+'/samples-intel'

                        cf['load_trees'] = '1';
                        cf['query_type'] = '3';
                        cf['route_method'] = '1';
                        cf['route_treesenabled'] = '3';
                        cf['route_disableshortcuts'] = '0';
                        cf['route_disablepayload'] = '0';
                        cf['query_args'] = '10 1000';

			if (est_sel == '0'):
                        	cf['query_invjoinsel'] = '5';
	                        cf['query_invsrcsel'] = '1 1';
			else:
				cf['query_invjoinsel'] = '1';
                                cf['query_invsrcsel'] = '1 1';

                        cf['query_samplingtime'] = '0 6553500';
                        cf['query_multicast'] = '0';
                        cf['query_prune'] = '0';
                        cf['query_samplecycles'] = '100';
                        cf['query_populatewindow'] = '0';
                        cf['query_windowsize'] = '1';
			cf['query_dimensions'] = '81 62';

			intel_testmethods = (('1','0','0'), ('0','0','0'), ('1','1','0'), ('1','0','1'));
			for m in test_methods:
	 			cf['query_sendtoroot'] = intel_testmethods[m][0];
	                        cf['config_enableght'] = intel_testmethods[m][1];
				cf['config_sigmod07'] = intel_testmethods[m][2];

	                        cf['route_begin'] = '1';
        	                cf['dir_route'] = 'joinroute-intel-q3-est/tc'+tci+'/method'+str(m);
                	        cf['dir_join'] = 'joinstate-intel-q3/tc'+tci+'/method'+str(m);
                        	print('Establishing paths...');
	                        execute_config(cf);

				cf['disable_debugout'] = '1';
        	                cf['route_begin'] = '0';
                	        cf['dir_route'] = 'joinroute-intel-q3-sampling/tc'+tci+'/method'+str(m);
                        	print('Sampling...');
	                        execute_config(cf);
				cf['disable_debugout'] = '0';


def route_intelstress(tc_index, test_methods, sample_rate):
	for top_name, n in top.iteritems():
                if 'intel' not in top_name:
			continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;

                for tci in tc_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci;
                        cf['dir_sample'] = top_dir+'/samples-intel'

                        cf['load_trees'] = '1';
                        cf['query_type'] = '3';
                        cf['route_method'] = '1';
                        cf['route_treesenabled'] = '3';
                        cf['route_disableshortcuts'] = '0';
                        cf['route_disablepayload'] = '0';
                        cf['query_args'] = '10 1000';

                        cf['query_invjoinsel'] = '5';
                        cf['query_invsrcsel'] = '1 1';

                        cf['query_multicast'] = '0';
                        cf['query_prune'] = '0';
                        cf['query_populatewindow'] = '0';
                        cf['query_windowsize'] = '1';
                        cf['query_dimensions'] = '81 62';

                        cf['retry_count'] = '20';
			cf['confirm_drop'] = '0';

                        intel_testmethods = (('1','0','0'), ('0','0','0'), ('1','1','0'), ('1','0','1'));
                        for m in test_methods:
                                cf['query_sendtoroot'] = intel_testmethods[m][0];
                                cf['config_enableght'] = intel_testmethods[m][1];
				cf['config_sigmod07'] = intel_testmethods[m][2];

                                cf['route_begin'] = '1';
                                cf['dir_route'] = 'joinroute-intel-q3-est/tc'+tci+'/method'+str(m);
                                cf['dir_join'] = 'joinstate-intel-q3/tc'+tci+'/method'+str(m);
                                print('Establishing paths...');
                                execute_config(cf);

                                for sr in sample_rate:
                                        cf['query_samplecycles'] = str(sr);
                                        cf['query_samplingtime'] = '0 '+str(sr*656);

                                        cf['route_begin'] = '0';
                                        cf['dir_route'] = 'joinroute-intel-q3-sampling/tc'+tci+'/sr'+str(sr)+'/method'+str(m);
                                        print('Sampling...');
                                        execute_config(cf);


def test_ghtjoinvar(test_topologies, tc_index, joinpointsel_config, join_sel, query_dim, query_type, window_size, sampling_time, sample_cycles):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;

                for tci in tc_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci;
                        cf['load_trees'] = '1';

                        set_joinquery(query_type, window_size, sampling_time, sample_cycles);

                        cf['route_begin'] = '0';
                        cf['query_sendtoroot'] = '0';
                        cf['query_prune'] = '0';

                        cf['config_enableght'] = '1';
                        cf['query_dimensions'] = query_dim;

                        for cf['query_invjoinsel'] in join_sel:
                                 for joinpoint_sel in joinpointsel_config:
                                        cf['route_begin'] = '1';
                                        cf['query_invsrcsel'] = str(joinpoint_sel[0])+' '+str(joinpoint_sel[1]);

					common_dirname = top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(joinpoint_sel[0])+'-'+str(joinpoint_sel[1])+'/ws'+str(window_size);
                                        cf['dir_join'] = 'joinstate-var-'+common_dirname;
                                        cf['dir_route'] = 'joinroute_est-var-'+common_dirname;

                                        print('Executing: '+cf['dir_route']);
                                        print('Establishing paths...');
                                        execute_config(cf);

                                        cf['route_begin'] = '0';
                                        cf['query_invsrcsel'] = str(joinpoint_sel[0])+' '+str(joinpoint_sel[1]);
                                        cf['dir_route'] = 'joinroute_sampling-var-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(joinpoint_sel[0])+'-'+str(joinpoint_sel[1])+'/ws'+str(window_size);

                                        print('Executing: '+cf['dir_route']);
                                        print('Sampling...');
                                        execute_config(cf);


def test_repairest(test_topologies, tc_index, srcsel_config, join_sel, test_methods, fail_count, fail_nodes, query_type, window_size, repair_level):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;

                for tci in tc_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci; 
                        cf['load_trees'] = '1';

                        cf['retry_count'] = '20';
			cf['repair_level'] = repair_level;

                        set_joinquery(query_type, window_size, 10000, 100);

                        for real_sel in srcsel_config:
                                cf['query_invsrcsel'] = str(real_sel[0])+' '+str(real_sel[1]);

                                for jointest_index in test_methods:
	                                method_settings = decode_method(jointest_index);
        	                        cf['query_sendtoroot'] = method_settings[0];
                	                cf['query_prune'] = method_settings[1];
                        	        cf['query_multicast'] = method_settings[2];
                                	cf['query_cachemulticast'] = method_settings[3];
	                                cf['merge_paths'] = method_settings[4];
        	                        cf['group_opt'] = method_settings[5];

                                        for cf['query_invjoinsel'] in join_sel:
                                                print('Establishing paths...');
                                                for fail_test in range(0, fail_count+1):
                                                        cf['fail_nodes'] = '';
                                                        ft = 0;

                                                        for node_id, fail_time in fail_nodes.iteritems():
                                                                if (ft == fail_test):
                                                                        break;
                                                                ft = ft+1;
                                                                cf['fail_nodes'] = cf['fail_nodes']+' '+str(node_id)+':'+str(fail_time);

                                                        cf['dir_join'] = 'joinstate-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/f'+str(fail_test)+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/ws'+str(window_size)+'/rl'+str(cf['repair_level']);
                                                        print('Executing: '+cf['dir_join']);

							print('Establishing paths...');
                                                        cf['route_begin'] = '1';
                                                        cf['dir_route'] = 'joinroute_est-'+top_name+'/tc'+tci+'/f'+str(fail_test)+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/ws'+str(window_size)+'/rl'+str(cf['repair_level']);
                                                        execute_config(cf);
	

def test_repairsampling(test_topologies, tc_index, srcsel_config, join_sel, test_methods, window_size, repair_level):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;

                for tci in tc_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci;
                        cf['load_trees'] = '1';

                        cf['retry_count'] = '20';
			cf['repair_level'] = repair_level;

                        set_joinquery('0', window_size, 10000, 100);
                        cf['query_type'] = '0';
                        cf['query_args'] = '44 47';

                        for real_sel in srcsel_config:
                                cf['query_invsrcsel'] = str(real_sel[0])+' '+str(real_sel[1]);

                                for jointest_index in test_methods:
	                                method_settings = decode_method(jointest_index);
        	                        cf['query_sendtoroot'] = method_settings[0];
                	                cf['query_prune'] = method_settings[1];
                        	        cf['query_multicast'] = method_settings[2];
                                	cf['query_cachemulticast'] = method_settings[3];
	                                cf['merge_paths'] = method_settings[4];
        	                        cf['group_opt'] = method_settings[5];

                                        for cf['query_invjoinsel'] in join_sel:
                                                cf['dir_join'] = 'joinstate-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/ws'+str(window_size);

                                                cf['route_begin'] = '1';
                                                cf['fail_nodes'] = '';
                                                cf['dir_route'] = 'joinroute_est-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/ws'+str(window_size);

                                                print('Executing: '+cf['dir_route']);
                                                execute_config(cf);
						
						try:
							f_jp = open(cf['dir_route']+'/join_nodes44.txt', 'r');
						except:
							print("Cannot open join_nodes44.txt");
							sys.exit();

						ll = f_jp.readlines();
						join_node = int(ll[len(ll)-1].split()[1]);
						print("Join node: "+str(join_node));
						f_jp.close();
						
                                                cf['route_begin'] = '0';
                                                cf['fail_nodes'] = '';
                                                cf['dir_route'] = 'joinroute_sampling-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/f0'+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/ws'+str(window_size);

                                                print('Executing: '+cf['dir_route']);
                                                execute_config(cf);

                                                for fail_test in range(0, 10):
                                                        cf['fail_nodes'] = str(join_node)+':'+str(4500+fail_test*100);
                                                        cf['dir_route'] = 'joinroute_sampling-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/f'+str(fail_test+1)+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/ws'+str(window_size);

                                                        print('Executing: '+cf['dir_route']);
                                                        print('Failed nodes: '+cf['fail_nodes']);
                                                        execute_config(cf);


def test_joinmethodsstress(test_topologies, tc_index, srcsel_config, join_sel, test_methods, sample_rate, query_type, window_size):
        for top_name, n in top.iteritems():
                if top_name not in test_topologies:
                        continue;

                cf['node_topology'] = top_dir+'/errors-'+top_name+'.txt';
                cf['node_count'] = str(n);
                cf['dir_value'] = top_dir+'/values-'+top_name;
		cf['confirm_drop'] = '0';	
	
                for tci in tc_index:
                        cf['dir_tree'] = tree_dir+'/trees-'+top_name+'/tc'+tci;
                        cf['load_trees'] = '1';

                        set_joinquery(query_type, window_size, 10000, 100);

                        for real_sel in srcsel_config:
                                cf['query_invsrcsel'] = str(real_sel[0])+' '+str(real_sel[1]);

                                for jointest_index in test_methods:
	                                method_settings = decode_method(jointest_index);
        	                        cf['query_sendtoroot'] = method_settings[0];
                	                cf['query_prune'] = method_settings[1];
                        	        cf['query_multicast'] = method_settings[2];
                                	cf['query_cachemulticast'] = method_settings[3];
	                                cf['merge_paths'] = method_settings[4];
        	                        cf['group_opt'] = method_settings[5];
					cf['config_enableght'] = method_settings[6];

                                        for cf['query_invjoinsel'] in join_sel:
                                                cf['dir_join'] = 'joinstate-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/ws'+str(window_size);
                                                print('Executing: '+cf['dir_join']);

                                                cf['route_begin'] = '1';
                                                cf['dir_route'] = 'joinroute_est-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/ws'+str(window_size);

                                                print('Establishing paths...');
                                                execute_config(cf);

                                                for sr in sample_rate:
                                                        cf['query_samplecycles'] = str(sr);
                                                        cf['query_samplingtime'] = '0 '+str(sr*100);
                                                        cf['retry_count'] = '10000';

                                                        cf['route_begin'] = '0';
                                                        cf['dir_join'] = 'joinstate-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/ws'+str(window_size);
                                                        cf['dir_route'] = 'joinroute_sampling-'+top_name+'/tc'+tci+'/qt'+cf['query_type']+'/sr'+str(sr)+'/js'+str(cf['query_invjoinsel'])+'/ss'+str(real_sel[0])+'-'+str(real_sel[1])+'/method'+str(jointest_index)+'/ws'+str(window_size);

                                                        print('Sample rate: '+str(sr));
                                                        execute_config(cf);



